import { defineStore } from "pinia"
import { REC_TYPE, CATE_TYPE } from '@utils/constant'
import { Banner } from "@/models/banner"
import { RecCourse, RecCoursePageSearch } from "@/models/course"
import { RecExpert, RecExpertPageSearch } from "@/models/expert"
import { RecMaterial, RecMaterialPageSearch } from "@/models/material"
import { Software } from "@/models/software"
import { userApi, bannerApi, courseApi, expertApi, materialApi, softwareApi, miscApi } from "@/services"

const useHomeStore  = defineStore('Home', {
    // state
    state:() =>({
        bannerListResult: [] as Banner[],
        expertListResult: [] as RecExpert[],
        materialListResult: [] as RecMaterial[],
        softwareListResult: [] as Software[],

        rumenCourseListResult: [] as RecCourse[],
        jinjieCourseListResult: [] as RecCourse[],
        gaojiCourseListResult: [] as RecCourse[],
        shizhanCourseListResult: [] as RecCourse[],
    }),

    // actions
    actions:{
        // 初始化数据
        async initData(){
            await this.getBanners();
            await this.getExperts();
            await this.getRecMatrials();
            await this.getRecSoftwares();

            this.rumenCourseListResult = await this.getCourses(CATE_TYPE.RUMEN);
            this.jinjieCourseListResult = await this.getCourses(CATE_TYPE.JINJIE);
            this.gaojiCourseListResult = await this.getCourses(CATE_TYPE.GAOJI);
            this.shizhanCourseListResult = await this.getCourses(CATE_TYPE.SHIZHAN);
        },

        // 获得banner列表
        async getBanners() {
            try {
                const res = await bannerApi.getBanners()
                this.bannerListResult = res? res : [] as Banner[]
            } catch(err) {
        
            }
        },

        // 获得精选课程列表
        async getCourses(cate: number) {
            try {
                const data = {
                    rec: REC_TYPE.TOPIC,
                    cate: cate,
                    pageNum: 1,
                    pageSize: 6,
                } as RecCoursePageSearch;
                const res = await courseApi.getRecCourses(data);
                return res? res.records : [] as RecCourse[]
            } catch(err) {
                return [] as RecCourse[]
            }
        },

        // 获得行家名师列表
        async getExperts() {
            try {
                const data = {
                    rec: REC_TYPE.TOPIC,
                    pageNum: 1,
                    pageSize: 16,
                } as RecExpertPageSearch;
                const res = await expertApi.getRecExperts(data);
                this.expertListResult = res? res.records : [] as RecExpert[]
            } catch(err) {
                
            }
        },

        // 获取资料列表
        async getRecMatrials() {
            try {
                const data : RecMaterialPageSearch = {
                    rec: REC_TYPE.TOPIC,
                    pageNum: 1,
                    pageSize: 6,
                };
                const res = await materialApi.getRecMaterials(data)
                this.materialListResult = res? res.records : [] as RecMaterial[]
            } catch(err) {
        
            }
        },

        // 获取软件列表
        async getRecSoftwares() {
            try {
                const res = await softwareApi.getRecSoftwares()
                this.softwareListResult = res? res : [] as Software[]
            } catch(err) {
        
            }
        },

        // 加为我的资料
        async addAsMyMaterial(id:number) {
            try {
                const res = await materialApi.addAsMyMaterial(id)
                return res? res.result : false
            } catch(err) {
                return false
            }
        },

        // 获取用户信息
        async getUserInfo() {
            try {
                const res = await userApi.getUserInfo(true)
                return res
            } catch(err) {
                
            }
        },

        // 获取Scnet跳转URL
        async getRedirectUrl(){
            try {
                const res = await miscApi.getScnetRedirectUrl()
                return res ? res.url : ""
            } catch(err) {
                return ""
            }
        },
    },
    getters:{
        bannerSource:(state) => state.bannerListResult,
        expertSorce:(state) => state.expertListResult,
        materialSource:(state) => state.materialListResult,
        softwareSource:(state) => state.softwareListResult,

        rumenCourseSource:(state) => state.rumenCourseListResult,
        jinjieCourseSource:(state) => state.jinjieCourseListResult,
        gaojiCourseSource:(state) => state.gaojiCourseListResult,
        shizhanCourseSource:(state) => state.shizhanCourseListResult,
    }
})

export default useHomeStore;